//加减
function min_number(){
    var count=document.getElementById("number");
    if(parseInt(count.value)>0){
        var result= parseInt(count.value)-1
        count.value=result
    }
}
function add_number(){
    var number=document.getElementById("number");
    var result=parseInt(number.value)+1
    number.value=result;
}
//获取token
window.onload=setTimeout("welcome()",1000);
function welcome() {
    var storage = window.localStorage;
    var user = storage.getItem("data");
    console.log(user);
    var user = JSON.parse(user);
    var username = user.user.userId;

    document.getElementById("username").innerHTML = username;
    console.log(username)
}

//获取所有商品信息并赋值给相应的元素
function getData() {
    $.ajax({
        type: "get",
        url: "/common/goods/list",
        contentType: "application/json;charset=UTF-8",
        dataType: "json",
        // data:data,
        success(res) {
            console.log(res.msg)
            if (2000 == res.code) {
                var goods = res.data.goods
                console.log(goods);
                //接收URL中的参数booksId
                var id = getUrlParam('goodsId');
                console.log('id:'+id);
                for(i=0;i<goods.length;i++){
                    if(id==goods[i].id){
                        var supplier_name=document.getElementById("supplier_name")
                        var goods_name=document.getElementById("goods_name")
                        var goods_id=document.getElementById("goods_id")
                        var city=document.getElementById("city")
                        var stock=document.getElementById("stock")
                        var description=document.getElementById("description")
                        var category=document.getElementById("category")
                        var store_price=document.getElementById("store_price")
                        var price=document.getElementById("price")
                        var start_time=document.getElementById("start_time")
                        var end_time=document.getElementById("end_time")
                        var goods_img=document.getElementById("goods_img")
                        var goods_link=document.getElementById("goods_link")
                        var time1 = datetime(goods[i].startTime);
                        var time2 = datetime(goods[i].endTime);
                        var buy=document.getElementById("buy")
                        buy.href="shopcart.html?goodsId="+goods_id
                        times=goods[i].startTime;//开始毫秒数
                        timee=goods[i].endTime;//结束毫秒数;
                        supplier_name.innerHTML=goods[i].supplierName
                        goods_name.innerHTML=goods[i].goodsName

                        buy.href="shopcart.html?goodsId="+goods[i].id

                        city.innerHTML=goods[i].city
                        stock.innerHTML=goods[i].stock
                        description.innerHTML=goods[i].description
                        category.innerHTML=goods[i].category
                        store_price.innerHTML=goods[i].storePrice
                        price.innerHTML=goods[i].price
                        start_time.innerHTML=time1
                        end_time.innerHTML=time2
                        goods_img.src=goods[i].imgPath
                        goods_link.href=goods[i].imgPath
                    }
                }
            }
        }
    })
}

//获取地址栏url中的参数，得到上一页传过来的id值
function getUrlParam(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
    var r = window.location.search.substr(1).match(reg); //匹配目标参数
    if (r != null) return unescape(r[2]); return null; //返回参数值
}

//格式化时间日期，输出年月日时分秒
function datetime(da) {
    da = new Date(da);
    var year = da.getFullYear()
    var month = da.getMonth() + 1
    var date = da.getDate()
    var hour =da.getHours()
    var minute=da.getMinutes()
    var second=da.getSeconds()
    return [year,month,date,hour,minute,second].join('\:')
}

//同步请求得到团购结束时间
var end = 0;
function getEnd() {
    $.ajax({
        type: "get",
        async: false,
        url: "/common/goods/list",
        contentType: "application/json;charset=UTF-8",
        dataType: "json",
        // data:data,
        success(res) {
            console.log(res.msg)
            if (2000 == res.code) {
                var goods = res.data.goods
                //接收URL中的参数booksId
                var id = getUrlParam('goodsId');
                for (i = 0; i < goods.length; i++) {
                    if (id == goods[i].id) {
                        end = goods[i].endTime//获取结束时间赋值给全局变量end
                    }
                }
            }
        }
    })

}
//倒计时功能,计算结束时间与当前时间的时间差，显示到页面
function clock() {
    var timee=end
    var times=new Date()
    if(timee>times){
        var time=document.getElementById("time")
        var temp=timee-times
        var clock=getClock(temp)//倒计时格式化
        time.innerHTML=clock
    }else {
        var time=document.getElementById("time")
        time.innerHTML="团购结束"
    }

}
//格式化倒计时，输出日时分秒
function getClock(da) {
    da = new Date(da)
    var date = da.getDate()+"天"
    var hour =da.getHours()+"时"
    var minute=da.getMinutes()+"分"
    var second=da.getSeconds()+"秒"
    return [date,hour,minute,second].join("")
}


//执行函数
window.onload=setTimeout("getData()",10);
window.onload=setTimeout("getEnd()",100);
setInterval("clock()","500")//每隔一秒更新时间差